package tabbedCanvas;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.util.ArrayList;

import drawPropertyCommands.DrawProperties;


/**
 * Each TrailLayer represents one draw for each turtle. This will allow us to draw with new properties
 * without erasing the old draws. These layers and put underneath the TurtleLayer so that the trails 
 * will always be under the turtles. 
 * 
 * @author KevinWang
 *
 */
public class TrailLayer extends CenteredLayer {
	
	private static final long serialVersionUID = 1L;
	private ArrayList<int[]> trail;
	private DrawProperties penProperties;
	
	
	public TrailLayer(Dimension d, DrawProperties drawProperties){
		super(d);
		penProperties=drawProperties;
		initializeTrail();
	}
	
	/**
	 * override the paintComponent method to draw trails
	 */
	@Override public void paintComponent(Graphics g) {
	    super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		
		penProperties.configure(g2);
		for(int [] segment: trail){
			g2.drawLine(segment[0], segment[1], segment[2], segment[3]);
		}
	}
	
	/**
	 * initializes the trail
	 */
	private void initializeTrail() {
		trail= new ArrayList<int[]>();
		int[] initialValue = {xCenter,yCenter, xCenter, yCenter};
		trail.add(initialValue);

	}
	
	/**
	 * draw a trail from x1,y1 to x2,y2
	 * @param x1 - x coordinate of original location
	 * @param y1 - y coordinate of original location
	 * @param x2 - x coordinate of final location
	 * @param y2 - y coordinate of final location
	 */
	public void draw (int x1, int y1, int x2, int y2){
		if(outOfBounds(x1, y1, x2, y2)){
			//TODO throw error
		}
		int[] newValue = {xCenter + x1, yCenter - y1, xCenter + x2, yCenter - y2};
		trail.add(newValue);
		repaint();
	}


	/**
	 * check to see if the given coordinates are out of bounds
	 * @param x1 - x coordinate of original location
     * @param y1 - y coordinate of original location
     * @param x2 - x coordinate of final location
     * @param y2 - y coordinate of final location
	 * @return whether it is out of bounds or not
	 */
	private boolean outOfBounds(int x1, int y1, int x2, int y2) {
		return Math.abs(x1)>dimension.getWidth()/2 || Math.abs(x2)> dimension.getWidth()/2
		||Math.abs(y1)> dimension.getHeight()/2 || Math.abs(y2)>dimension.getHeight()/2;
	}
	
	/**
	 * return current Trail for condensing purposes
	 * @return
	 */
	public ArrayList<int[]> getTrail(){
	    return trail;
	}
}
